<?php

namespace app\webactive\controller;

use app\common\lib\exception\ApiException;
use think\Controller;
use think\response\Json;
use think\Session;

class AppCoinRecharge extends Controller
{
    // 用户ID
    protected $uid = 0;
    // APP类型 1=>准了APP  2=>准有好事APP
    protected $appType = 0;

    protected function _initialize()
    {
        $this->uid = (int)session('h5_coin_recharge_uid');
        $this->appType = (int)session('h5_coin_recharge_app_type');

        $action = strtolower($this->request->action());
        if (in_array($action, ['index', 'loginout', 'saveorder']))
        {
            if (!$this->uid)
            {
                throw new ApiException('请先登录', 200, 405);
            }
        }
        if (in_array($action, ['getcode', 'tellogin', 'loginout', 'saveorder'], true) && !$this->request->isPost())
        {
            throw new ApiException('服务异常，请稍后再试～', 200, 0);
        }
        if (in_array($action, ['index'], true) && !$this->request->isGet())
        {
            throw new ApiException('服务异常，请稍后再试～', 200, 0);
        }
    }

    public function getCode()
    {
        $appType = input('app_type/d', 0);
        $areaCode = input('area_code/d', 0);
        $tel = input('tel/s', 0);

        if (!in_array($appType, [1, 2], true) || !$areaCode || !$tel)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $res = (new \app\webactive\service\AppCoinRecharge())->getCode($areaCode, $tel, $appType);

        if ($res['success'])
        {
            return show(config('code.success'), '请求成功');
        } else
        {
            return show(config('code.error'), $res['msg']);
        }
    }

    public function telLogin()
    {
        $appType = input('app_type/d', 0);
        $areaCode = input('area_code/d', 0);
        $tel = input('tel/s', 0);
        $code = input('code/s', 0);
        $isReg = input('is_reg/d', 0);

        if (!in_array($appType, [1, 2], true) || !$areaCode || !$tel || !$code)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $isRegister = $isReg === 1 ? true : false;
        if ($appType === 1)
        {
            $isRegister = true;
        }
        $res = (new \app\webactive\service\AppCoinRecharge())->telLogin($areaCode, $tel, $code, $isRegister, $appType);

        if (!$res['success'] && $res['status'] === 0)
        {
            return show(config('code.error'), $res['msg']);
        } else
        {
            if ($res['success'])
            {
                $msg = '登录成功';
                $status = 1;

                Session::set('h5_coin_recharge_uid', $res['uid']);
                Session::set('h5_coin_recharge_app_type', $appType);
            } else
            {
                $msg = $res['msg'];
                $status = 2;
            }

            return show(config('code.success'), $msg, [
                'status' => $status,
            ]);
        }
    }

    public function index()
    {
        $appType = input('app_type/d', 0);
        if ($appType && $this->appType !== $appType)
        {
            return show(405, '请先登录');
        }

        $infoData = (new \app\webactive\service\AppCoinRecharge())->index($this->uid, $this->appType);

        return show(config('code.success'), '请求成功', $infoData);
    }

    /**
     * 退出登录
     * @return Json
     */
    public function loginOut()
    {
        Session::delete('h5_coin_recharge_uid');
        Session::delete('h5_coin_recharge_app_type');

        return show(config('code.success'), '退出登录成功');
    }

    /**
     * 下单
     * @return Json
     * @throws ApiException
     */
    public function saveOrder()
    {
        $appType = input('app_type/d', 0);
        if ($appType && $this->appType !== $appType)
        {
            return show(405, '请先登录');
        }
        $tardeType = input('trade_type/d', 0);
        $gid = input('gid/d', 0);
        $openid = input('openid/s', '');
        $callback_url = input('callback_url/s', '');
        if (!in_array($tardeType, [6, 7, 8], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        if (!$openid && in_array($tardeType, [6, 8], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $res = (new \app\webactive\service\AppCoinRecharge())->saveOrder($this->uid, $tardeType, $gid, $this->appType, $callback_url, $openid);
        if ($res['status'] === false)
        {
            return show(config('code.error'), $res['message']);
        }
        return show(config('code.success'), '下单成功～', $res['data']);
    }
}
